package com.sctvcloud.beichuan.ui.activities;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.AbsRunnable;
import com.ruihang.generalibrary.ui.util.ViewUtils;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.NetModeUtil;
import com.sctvcloud.beichuan.R;
import com.sctvcloud.beichuan.base.BaseActivity;
import com.sctvcloud.beichuan.base.BaseCanPlayActivity;
import com.sctvcloud.beichuan.base.BaseFragment;
import com.sctvcloud.beichuan.beans.BroadcastItem;
import com.sctvcloud.beichuan.beans.HomeLive;
import com.sctvcloud.beichuan.beans.TodayNews;
import com.sctvcloud.beichuan.http.AbsNetCallBack;
import com.sctvcloud.beichuan.http.NetUtils;
import com.sctvcloud.beichuan.ui.fragment.GreatestFragment;
import com.sctvcloud.beichuan.ui.utils.GlideUtil;
import com.sctvcloud.beichuan.ui.utils.OnFullClick;
import com.sctvcloud.beichuan.ui.utils.SampleListener;
import com.sctvcloud.beichuan.ui.utils.TitleUtils;
import com.sctvcloud.beichuan.ui.widget.BasePlayerView;
import com.shuyu.gsyvideoplayer.listener.INetPromt;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 预警详情页
 */
public class BroadcastActivity extends BaseCanPlayActivity  {
    private static boolean isMute = false;
    @BindView(R.id.player_main_live_video)
    protected BasePlayerView videoView;
    @BindView(R.id.living_img_video)
    protected CustomEXImageView topImg;
    @BindView(R.id.player_live_home_lay)
    protected ViewGroup videoContain;
    @BindView(R.id.tv_emer)
    protected TextView tv_emer;

    private BaseFragment frag;

    private ImageView imageView;

    private List<CustomEXImageView> images;


    private Handler handler;
    protected String channelUrl;

    @BindView(R.id.title_top_layout_edit)
    protected CustomFontTextView share;

    @BindView(R.id.title_top_logo_layout)
    protected LinearLayout title_top_logo_layout;
    @BindView(R.id.title_top_title)
    protected CustomFontTextView title_top_title;
    @BindView(R.id.title_top_layout_back)
    protected ImageView title_top_layout_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);
        ButterKnife.bind(this);
        if(null!= getIntent() && !TextUtils.isEmpty(getIntent().getStringExtra(BaseActivity.EX_URL))) {
            channelUrl = getIntent().getStringExtra(BaseActivity.EX_URL);
        }
        new TitleUtils(this);

        title_top_logo_layout.setVisibility(View.GONE);
        title_top_title.setVisibility(View.VISIBLE);
        title_top_title.setText(getText(R.string.text_jinji));
        title_top_layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        share.setVisibility(View.GONE);
        getData();
    }


    private void initPlay(String liveUrl ,String img){

        handler = new Handler();
        images = new ArrayList<>();
           ViewUtils.resetTo169Size(videoContain);
        imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        GlideUtil.getGlideWithLarge169DefNotFit(this, img).into(imageView);
        videoView.setThumbImageView(imageView);

        if (isMute) {
            videoView.setMute(isMute);
        }
        videoView.setIsTouchWiget(false);
        videoView.setLiving(true);

        videoView.setRadio(true);



        videoView.setMuteChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isMute = isChecked;
            }
        });
        videoView.setMKeepScreenOn(false);

        if (null != frag && frag instanceof GreatestFragment) {
            ((GreatestFragment) frag).initPlayer(videoView);
        }

        videoView.setNetPromt(new INetPromt() {
            @Override
            public boolean onNetPromt() {

                if (null != frag && frag instanceof GreatestFragment) {
                    ((GreatestFragment) frag).showNetDialog(videoView);
                } else {
                    videoView.startPlayLogic();
                }
                return false;
            }
        });

        videoView.setStandardVideoAllCallBack(new SampleListener() {
            @Override
            public void onPrepared(String url, Object... objects) {
                super.onPrepared(url, objects);
            }
        });
        videoView.setOnPlayerStatusListener(new BasePlayerView.OnPlayerStatusChanged() {
            @Override
            public void OnPlaying() {

            }

            @Override
            public void OnError() {

            }

            @Override
            public void OnStop() {

            }

            @Override
            public void OnEnd() {

            }
        });

        videoView.setFullClick(new OnFullClick() {
            @Override
            public void onFullClick(StandardGSYVideoPlayer player, boolean isLandscape) {
                JLog.e("vvav  full click isLandScape=" + isLandscape + "   player=" + player);
                onFullClicked(player, isLandscape);
            }
        });

        postPlay(liveUrl);

    }

    private void getData() {
        NetUtils.getNetAdapter().getBroadcast(getOwnerName(), channelUrl, new AbsNetCallBack<BroadcastItem>(TodayNews.class) {
            @Override
            public void onSuc(BroadcastItem organDetal) {

                if(!TextUtils.isEmpty(organDetal.getThirdPartyUrl())){
                    initPlay(organDetal.getThirdPartyUrl(),organDetal.getThirdPartyIcon());
                }
                if(!TextUtils.isEmpty(organDetal.getBroadCastTitle())) {
                    tv_emer.setText(organDetal.getBroadCastTitle());
                }

            }

            @Override
            public void onEnd() {
                super.onEnd();
            }
        });
    }

    @Override
    protected void onDestroy() {
        if(videoView!=null && videoView.isPlaying()){
            videoView.release();
        }
        super.onDestroy();
    }

    private void postPlay(String url) {
        if (videoView != null && !videoView.isPlaying() && videoView.getWidth() <= 0) {
            handler.postDelayed(new AbsRunnable(url) {
                @Override
                public void run() {
                    play((String) obj);
                }
            }, 500);
        } else {
            play(url);
        }
    }


    private void play(String url) {
        if (TextUtils.isEmpty(url)) {

            return;
        }
        if (videoView != null) {
            if (videoView.isPlaying()) {
                videoView.onVideoPause();
            }

            videoView.setMute(isMute);
            videoView.setUp(url, false, "");
            int net = NetModeUtil.GetNetype(this);
            if (net == 1) {
                videoView.startPlayLogic();
            } else {
            }
        }


    }


}
